/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.player.abilities;

import static com.cmp.core.constants.InputUIDConstants.*;
import com.cmp.player.controls.CharacterRigidBodyControl;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;

/**
 * the ability to jump.
 * @author krummer
 */
public class JumpControl extends AbilityControl {
    private static final String JUMP_SPEED_KEY = "jumpSpeed";
    private CharacterRigidBodyControl body;
    private boolean isJump;

    @Override
    public void execute() {
        if(body.isOnGround()) {
            
            body.setLinearVelocity(body.getLinearVelocity().setY(this.getJumpSpeed()));
        }
    }
    
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        
        super.getSpatial().setUserData(JUMP_BUTTON_UID, false);
        body = spatial.getControl(CharacterRigidBodyControl.class);
        setJumpSpeed(6.f);
    }
    
    public void setJumpSpeed(float speed) {
        super.getSpatial().setUserData(JUMP_SPEED_KEY, speed);
    }
    
    public float getJumpSpeed() {
        return super.getSpatial().getUserData(JUMP_SPEED_KEY);
    }

    @Override
    protected void controlUpdate(float tpf) {
        isJump = super.getSpatial().getUserData(JUMP_BUTTON_UID);
        if(isJump) {
            execute();
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
